<template>
  <div class="basic-setting-content pl16 pr16">
    <!--基本信息-->
    <div class="common-form">基本信息</div>
    <el-form-item
      label="商品名称："
      :rules="[{ required: true, message: '请填写商品名称' }]"
      prop="model.productName"
    >
      <el-input v-model="form.model.productName" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item label="商品编码：">
      <el-input v-model="form.model.productNo" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item
      label="所属分类："
      :rules="[{ required: true, message: '请选择商品分类' }]"
      prop="model.categoryId"
    >
      <el-select v-model="form.model.categoryId" filterable clearable>
        <template v-for="cat in form.category" :key="cat.categoryId">
          <el-option :value="cat.categoryId" :label="cat.name"></el-option>
          <template v-for="two in cat.children" :key="two.categoryId">
            <el-option
              :value="two.categoryId"
              :label="two.name"
              style="padding-left: 30px"
            ></el-option>
          </template>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item
      label="贸易方式："
      :rules="[{ required: true, message: '请选择商品分类' }]"
      prop="model.tradeTypeId"
    >
      <el-select
        v-model="form.model.tradeTypeId"
        filterable
        clearable
        placeholder="请选择"
      >
        <el-option
          v-for="item in myfsList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item
      label="所属品牌："
      :rules="[{ required: true, message: '请选择所属品牌' }]"
      prop="model.brandId"
    >
      <el-select
        v-model="form.model.brandId"
        filterable
        clearable
        placeholder="请选择"
      >
        <el-option
          v-for="item in ssppList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item
      label="发货仓："
      :rules="[{ required: true, message: '请选择发货仓' }]"
      prop="model.ware"
    >
      <el-select
        v-model="form.model.ware"
        filterable
        clearable
        placeholder="请选择"
      >
        <el-option
          v-for="item in fhcList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <!-- // 是否新品
    newProduct: 0,-->
    <!-- // 左上角是否显示
    leftUpShow: 0,
    // 左上角显示内容
    leftUpContent: "",
    // 商品标签
    labels: [],
    // 贸易方式
    tradeTypeId: null,
    // 所属品牌
    brandId: null,-->
    <el-form-item label="是否新品：">
      <el-radio-group v-model="form.model.newProduct">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="是否线上商品：">
      <el-radio-group v-model="form.model.onlineProduct">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      v-if="form.model.onlineProduct == 0"
      label="所属门店："
      :rules="[{ required: true, message: '请选择所属门店' }]"
      prop="model.storeId"
    >
      <el-select
        style="width: 460px"
        v-model="form.model.storeId"
        filterable
        clearable
        placeholder="请选择"
      >
        <el-option
          v-for="item in storeList"
          :key="item.storeId"
          :label="item.storeName"
          :value="item.storeId"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item
      v-if="form.model.storeId"
      prop="model.storeSupplier"
      label="门店供应商："
    >
      <el-input v-model="form.model.storeSupplier" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item label="是否含税：">
      <el-radio-group v-model="form.model.tax">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      prop="model.taxPercent"
      label="税率"
      v-if="form.model.tax == 0"
      :rules="[{ required: true, message: '请填写税率' }]"
    >
      <el-input
        v-model="form.model.taxPercent"
        type="number"
        class="max-w460"
      ></el-input>
    </el-form-item>
    <el-form-item label="hscode">
      <el-input
        v-model="form.model.hscode"
        type="number"
        class="max-w460"
      ></el-input>
    </el-form-item>
    <el-form-item label="是否展示左上角图标：">
      <el-radio-group v-model="form.model.leftUpShow">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="上传左上角图标" v-if="form.model.leftUpShow == 1">
      <el-row>
        <div class="draggable-list">
          <div
            class="item img-select"
            v-if="form.model.leftUpContent == ''"
            @click="openProductUpload('image', 'leftImage')"
          >
            <el-icon>
              <Plus />
            </el-icon>
          </div>
          <div
            v-if="form.model.leftUpContent != ''"
            class="item"
            @click="openProductUpload('image', 'leftImage')"
          >
            <img :src="form.model.leftUpContent" width="100" height="100" />
          </div>
        </div>
      </el-row>
      <div class="gray9">建议左上角图标尺寸为750x750</div>
    </el-form-item>
    <el-form-item label="商品标签：" prop="tags">
      <div class="mb20">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handlerAddTags"
          >添加</el-button
        >
      </div>
      <div class="tags">
        <div
          v-for="(item, index) in form.model.labels"
          :key="index"
          class="mb10 mr50 selWidth200"
          style="margin: 10px"
        >
          <el-input
            v-model="form.model.labels[index].name"
            class="tagsInner"
          ></el-input>
          <el-button
            style="margin-left: 20rpx"
            type="danger"
            @click="handlerDeleteTags(index)"
            icon="el-icon-delete"
            circle
            >删除</el-button
          >
          <!-- <el-button
           
            circle
            type="danger"
            icon="el-icon-delete"
            class="ml10"
            size="mini"
          ></el-button>-->
        </div>
      </div>
    </el-form-item>
    <el-form-item label="销售状态：">
      <el-radio-group v-model="form.model.productStatus">
        <el-radio :label="10">立即上架</el-radio>
        <el-radio :label="20">放入仓库</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="预告商品：">
      <el-radio-group v-model="form.model.isPreview">
        <el-radio :label="1">开启</el-radio>
        <el-radio :label="0">关闭</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      v-if="form.model.isPreview == 1"
      label="预告开启购买时间"
      :rules="[{ required: true, message: '请选择开启购买时间' }]"
      prop="model.previewTime"
    >
      <el-date-picker
        v-model="form.model.previewTime"
        type="datetime"
        value-format="YYYY-MM-DD HH:mm:ss"
        placeholder="选择日期"
      ></el-date-picker>
    </el-form-item>
    <el-form-item
      label="商品图片："
      :rules="[{ required: true, message: '请上传商品图片' }]"
      prop="model.image"
    >
      <div class="draggable-list">
        <draggable class="wrapper" v-model="form.model.image">
          <template #item="{ element, index }">
            <div class="item">
              <img v-img-url="element.filePath" />
              <a
                href="javascript:void(0);"
                class="delete-btn"
                @click.stop="deleteImg(index)"
              >
                <el-icon>
                  <Close />
                </el-icon>
              </a>
            </div>
          </template>
        </draggable>
        <div
          class="item img-select"
          @click="openProductUpload('image', 'image')"
        >
          <el-icon>
            <Plus />
          </el-icon>
        </div>
      </div>
      <div class="gray9">建议图片尺寸为750x750</div>
    </el-form-item>
    <el-form-item label="商品视频：">
      <el-row>
        <div class="draggable-list">
          <div
            class="item img-select"
            v-if="form.model.videoId == 0"
            @click="openProductUpload('video', 'video')"
          >
            <el-icon>
              <Plus />
            </el-icon>
          </div>
          <div v-if="form.model.videoId != 0">
            <video
              width="150"
              height="150"
              :src="form.model.videoFilePath"
              :autoplay="false"
              controls
            >
              您的浏览器不支持 video 标签
            </video>
            <div>
              <el-button icon="Picture" @click="delVideo">删除视频</el-button>
            </div>
          </div>
        </div>
      </el-row>
    </el-form-item>
    <el-form-item label="视频封面：">
      <el-row>
        <div class="draggable-list">
          <div
            class="item img-select"
            v-if="form.model.posterId == 0"
            @click="openProductUpload('image', 'poster')"
          >
            <el-icon>
              <Plus />
            </el-icon>
          </div>
          <div
            v-if="form.model.posterId != 0"
            class="item"
            @click="openProductUpload('image', 'poster')"
          >
            <img :src="form.model.posterFilePath" width="100" height="100" />
          </div>
        </div>
      </el-row>
      <div class="gray9">建议图片尺寸为750x750</div>
    </el-form-item>
    <el-form-item label="商品卖点：">
      <el-input
        type="textarea"
        v-model="form.model.sellingPoint"
        class="max-w460"
      ></el-input>
    </el-form-item>
    <!--其他设置-->
    <!-- <div class="common-form mt50">其他设置</div>
    <el-form-item label="商品属性：">
      <el-radio-group v-model="form.model.isVirtual">
        <el-radio :label="0">实物商品</el-radio>
        <el-radio :label="1">虚拟商品(无需发货)</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="运费模板：" :rules="[{ required: true, message: '选择运费模板' }]" prop="model.deliveryId" v-if="form.model.isVirtual == 0">
      <el-select v-model="form.model.deliveryId">
        <el-option v-for="item in form.delivery" :value="item.deliveryId" :key="item.deliveryId" :label="item.name"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="初始销量："><el-input type="number" min="0" v-model="form.model.salesInitial" class="max-w460"></el-input></el-form-item>
    <el-form-item label="商品排序：" :rules="[{ required: true, message: ' ' }]" prop="model.productSort">
      <el-input type="number" min="0" v-model="form.model.productSort" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item label="限购数量：" :rules="[{ required: true, message: ' ' }]" prop="model.limitNum">
      <el-input type="number" min="0" v-model="form.model.limitNum" class="max-w460"></el-input>
      <div class="gray9">每个会员购买的最大数量，0为不限购</div>
    </el-form-item>
    <el-form-item label="发货类型：" v-if="form.model.isVirtual == 1">
      <el-radio-group v-model="form.model.virtualAuto">
        <el-radio :label="1">自动</el-radio>
        <el-radio :label="0">手动</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="虚拟内容：" :rules="[{ required: true, message: '请填写虚拟内容' }]" prop="model.virtualContent" v-if="form.model.isVirtual == 1">
      <el-input type="text" v-model="form.model.virtualContent" class="max-w460"></el-input>
      <div class="gray9">虚拟物品内容</div>
    </el-form-item>
    <el-form-item label="会员等级限制：">
      <el-select v-model="form.model.gradeIds" multiple placeholder="请选择" style="width: 460px;">
        <el-option v-for="item in form.gradeList" :key="item.gradeId" :label="item.name" :value="item.gradeId"></el-option>
      </el-select>
      <div class="gray9">仅设置的等级会员可购买，不设置则都可以购买</div>
    </el-form-item>-->
    <!--商品图片组件-->
    <Upload
      v-if="isProductUpload"
      :config="config"
      :isupload="isProductUpload"
      @returnImgs="returnProductImgsFunc"
      >上传图片</Upload
    >
  </div>
</template>

<script>
import PorductApi from "@/api/product.js";
import Upload from "@/components/file/Upload.vue";
import draggable from "vuedraggable";
import StoreApi from "@/api/store.js";
export default {
  components: {
    Upload,
    draggable,
  },
  data() {
    return {
      fhcList: [
        {
          id: 1,
          name: "门店",
        },
        {
          id: 2,
          name: "公共仓",
        },
        {
          id: 3,
          name: "唯品会仓",
        },
        {
          id: 4,
          name: "行云仓",
        },
      ],
      ssppList: [],
      myfsList: [],
      isProductUpload: false,
      config: {},
      file_name: "image",
      storeList: [],
    };
  },
  inject: ["form"],
  created() {
    this.getStoreList();
    this.init();
  },
  methods: {
    getStoreList() {
      StoreApi.shoplist(
        {
          pageIndex: 1,
          pageSize: 99999,
        },
        true
      )
        .then((res) => {
          this.storeList = res.data.records;
        })
        .catch((error) => {});
    },
    init() {
      PorductApi.tradeTypeIndex().then((res) => {
        this.myfsList = res.data.records;
      });
      let data = {
        pageIndex: 1,
        pageSize: 999999,
      };
      PorductApi.brandIndex(data).then((res) => {
        this.ssppList = res.data.records;
      });
    },
    // init() {

    //   brandIndex
    //   http://118.178.197.92/api/supplier/product/tradeType/index
    // },
    //添加标签
    handlerAddTags() {
      this.form.model.labels.push({ name: "" });
    },
    //删除标签
    handlerDeleteTags(index) {
      this.form.model.labels.splice(index, 1);
    },
    /*打开上传图片*/
    openProductUpload: function (file_type, file_name) {
      this.file_name = file_name;
      if (file_type == "image") {
        this.config = {
          total: 9,
          fileType: "image",
        };
      } else {
        this.config = {
          total: 1,
          fileType: "video",
        };
      }
      this.isProductUpload = true;
    },

    /*上传商品图片*/
    returnProductImgsFunc(e) {
      if (e != null) {
        if (this.file_name == "video") {
          this.form.model.videoId = e[0].fileId;
          this.form.model.videoFilePath = e[0].filePath;
        } else if (this.file_name == "image") {
          let imgs = this.form.model.image.concat(e);
          // 兼容后端
          for (let i = 0; i < imgs.length; i++) {
            if (typeof imgs[i].imageId == "undefined") {
              imgs[i].imageId = imgs[i].fileId;
            }
          }
          this.form.model.image = imgs;
        } else if (this.file_name == "poster") {
          this.form.model.posterId = e[0].fileId;
          this.form.model.posterFilePath = e[0].filePath;
        } else if (this.file_name == "leftImage") {
          this.form.model.leftUpContent = e[0].fileId;
          this.form.model.leftUpContent = e[0].filePath;
          // console.log(this.form.model.leftImage, "this.form.、model.leftImage");
        }
      }
      this.isProductUpload = false;
    },

    /*删除商品图片*/
    deleteImg(index) {
      this.form.model.image.splice(index, 1);
    },
    delVideo() {
      this.form.model.videoId = 0;
      this.form.model.video = {};
    },
  },
};
</script>

<style lang="scss" scoped>
.tags {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .tagsInner {
    width: 150px;
  }
}
</style>
